package com.jjw.springbootcvs.controller;


import com.jjw.springbootcvs.config.BootCvsProperties;
import com.jjw.springbootcvs.pojo.entity.SysUser;
import com.jjw.springbootcvs.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author xiaojuan
 * @Date 2024/11/1
 * @Description 用户登录控制器
 */
@Controller
@RequiredArgsConstructor
public class LoginController {

    private final BootCvsProperties bootCvsProperties;

    private final  UserService userService;

    /**
     * 用户登录
     * @param userName
     * @param password
     * @param session
     * @param model
     * @return
     */
    @PostMapping(value = "/logins")
    public String login(@RequestParam("account") String userName, @RequestParam("password")String password, HttpSession session, Model model) {
        SysUser login = userService.login(userName, password);

        try {
            login.setPassword("******");
            session.setAttribute(bootCvsProperties.getSessionKey(), login);
            session.setMaxInactiveInterval(bootCvsProperties.getSessionTimeout());
        } catch (Exception e) {
            model.addAttribute("error", "登录失败");
            return "login";
        }
        return "frame";
    }


}
